/*
 * Created on :2012-9-25 Author :focus.huang
 */
package com.dmagic.cms.common.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nutz.json.Json;
import org.springside.modules.mapper.JsonMapper;

import com.fasterxml.jackson.annotation.JsonInclude.Include;

public class JsonUtil {
    private static final Log logger = LogFactory.getLog(JsonUtil.class);

    /**
     * @description : fromJson
     * @author focus.huang
     * @param jsonString
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> fromJson(String jsonString) {
        jsonString = decodeUrl(jsonString);
        return JsonMapper.nonEmptyMapper().fromJson(jsonString, HashMap.class);
    }

    /**
     * @description : fromJson
     * @author focus.huang
     * @param json
     * @param clazz
     * @return
     */
    public static <T> T fromJson(String json, Class<T> clazz) {
        json = decodeUrl(json);
        return JsonMapper.nonEmptyMapper().fromJson(json, clazz);
    }

    /**
     * @description : toJson
     * @author focus.huang
     * @param obj
     * @return
     */
    public static String toJson(Object obj) {
        String result = "";
        if (obj == null)
            return result;
        try {
            result = JsonMapper.nonEmptyMapper().toJson(obj);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result;

    }

    /**
     * @description : toFullJson
     * @author focus.huang
     * @param obj
     * @return
     */
    public static String toFullJson(Object obj) {
        String result = "";
        if (obj == null)
            return result;
        try {
            JsonMapper jm = new JsonMapper();
            result = jm.toJson(obj);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result;

    }

    public static String toJsonWithDateFormat(Object obj, String format) throws Exception {
        if (StringUtil.isNotBlank(format)) {
            JsonMapper jm = new JsonMapper(Include.NON_EMPTY);
            SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.ENGLISH);
            return jm.getMapper().setDateFormat(dateFormat).writeValueAsString(obj);
        }
        return "";
    }

    public static String toFullJsonWithDateFormat(Object obj, String format) throws Exception {
        if (StringUtil.isNotBlank(format)) {
            JsonMapper jm = new JsonMapper(Include.ALWAYS);
            SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.ENGLISH);
            return jm.getMapper().setDateFormat(dateFormat).writeValueAsString(obj);
        }
        return "";
    }

    /**
     * @description : decode Url
     * @param url
     * @return
     */
    private static String decodeUrl(String url) {
        try {
            return URLDecoder.decode(url, "utf-8");
        } catch (UnsupportedEncodingException e) {
            return url;
        } catch (IllegalArgumentException e) {
            logger.debug(e.getMessage());
            return url;
        }
    }

    public static String toJsonWithFormat(Object obj) {
        String result = "";
        if (obj == null)
            return result;
        try {
            result = Json.toJson(obj);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result;

    }
}
